package com.crm.service.impl.contactManager;


import com.crm.common.model.Enum.StatusCodeEnum;
import com.crm.dao.mapper.contactManager.ContactDetailMapper;
import com.crm.dao.mapper.contactManager.ContactMapper;
import com.crm.dao.mapper.contactManager.ContactTagMapper;
import com.crm.dao.mapper.customMapper.contactManagerCustomMapper.ContactCustomMapper;
import com.crm.model.entity.contactManager.ContactDetail;
import com.crm.model.entity.contactManager.ContactDetailExample;
import com.crm.model.entity.contactManager.ContactTag;
import com.crm.model.entity.contactManager.ContactTagExample;
import com.crm.service.contactManager.ContactDetailDbService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 联系人管理信息
 */
@Service
public class ContactDetailDbServiceImpl implements ContactDetailDbService {
    @Autowired
    private ContactMapper contactMapper;

    @Autowired
    private ContactDetailMapper contactDetailMapper;

    @Autowired
    private ContactTagMapper contactTagMapper;


    /**
     * 根据联系人id查询同一联系人下的联系方式
     * @param order 排序字段,类型
     * @param contactId 联系人id
     * @param contactDetailType 联系人详情类型  0联系方式 1节日节点
     * @return
     */
    @Override
    public List<ContactDetail> contactDetailList(String order, String contactId,Integer contactDetailType) {
        ContactDetailExample contactDetailExample = new ContactDetailExample();
        contactDetailExample.setOrderByClause(order);
        ContactDetailExample.Criteria criteria = contactDetailExample.createCriteria();
        criteria.andContactIdEqualTo(contactId);
        criteria.andContactDetailTypeEqualTo(contactDetailType);
        criteria.andDeleteStatusEqualTo(Short.parseShort(String.valueOf(StatusCodeEnum.NORMAL_CODE.getState())));
        List<ContactDetail> list = contactDetailMapper.selectByExample(contactDetailExample);
        return list;
    }

    /**
     * 新增联系方式,,节日节点
     * @param contactDetail 实体类
     * @return
     */
    @Override
    public Boolean addContactDetail(ContactDetail contactDetail) {
        Boolean result = false;
        int num = contactDetailMapper.insertSelective(contactDetail);
        if(num>0){
            result = true;
        }
        return result;
    }

    /**
     * 联系人-联系人详情-联系方式重复
     * @param contactId 联系人id
     * @param contactDetailValue 联系方式
     * @return
     */
    @Override
    public ContactDetail getContactDetailIdAndContactDetailValue(String contactId, String contactDetailValue) {
        ContactDetailExample contactDetailExample = new ContactDetailExample();
        ContactDetailExample.Criteria criteria = contactDetailExample.createCriteria();
        criteria.andContactIdEqualTo(contactId);
        criteria.andContactDetailValueEqualTo(contactDetailValue);
        criteria.andDeleteStatusEqualTo(Short.parseShort(String.valueOf(StatusCodeEnum.NORMAL_CODE.getState())));
        List<ContactDetail> list = contactDetailMapper.selectByExample(contactDetailExample);
        if(list.size() == 0) {
            return new ContactDetail();
        }else{
            return list.get(0);
        }

    }

    /**
     * 联系人-修改联系方式,节日节点
     * @param contactDetail 实体类
     * @return
     */
    @Override
    public Boolean updateContactDetail(ContactDetail contactDetail) {
        Boolean result = false;
        int num = contactDetailMapper.updateByPrimaryKeySelective(contactDetail);
        if(num>0){
            result = true;
        }
        return result;
    }

    /**
     * 联系人-联系人详情-联系方式,节日节点修改回显
     * @param contactDetailId 联系方式id,节日节点id
     * @return
     */
    @Override
    public ContactDetail getContactDetailId(String contactDetailId) {
        return contactDetailMapper.selectByPrimaryKey(contactDetailId);
    }

    /**
     * 查询联系人标签
     * @return
     */
    @Override
    public List<ContactTag> contactTaList(String accountId) {
        ContactTagExample contactTagExample = new ContactTagExample();
        contactTagExample.setOrderByClause("create_tm desc");
        ContactTagExample.Criteria criteria = contactTagExample.createCriteria();
        criteria.andCreatePeopleIdEqualTo(accountId);
        criteria.andDeleteStatusEqualTo(0);
        List<ContactTag> list = contactTagMapper.selectByExample(contactTagExample);
        return list;
    }

    /**
     * 删除联系人标签库
     * @return
     */
    @Override
    public Boolean updateContactTag(ContactTag tag) {
        Boolean result = false;
        int num = contactTagMapper.updateByPrimaryKeySelective(tag);
        if(num>0){
            result = true;
        }
        return result;
    }

    /**
     * 删除联系人标签库
     * @return
     */
    @Override
    public Boolean addContactTag(ContactTag tontactTag) {
        Boolean result = false;
        int num = contactTagMapper.insertSelective(tontactTag);
        if(num>0){
            result = true;
        }
        return result;
    }

    /**
     * 查询联系人标签
     * @return
     */
    @Override
    public ContactTag contactTagList(String tag) {
        ContactTagExample contactTagExample = new ContactTagExample();
        contactTagExample.setOrderByClause("create_tm desc");
        ContactTagExample.Criteria criteria = contactTagExample.createCriteria();
        criteria.andTagEqualTo(tag);
        criteria.andDeleteStatusEqualTo(0);
        List<ContactTag> list = contactTagMapper.selectByExample(contactTagExample);
        if(list != null && list.size() > 0){
            return list.get(0);
        }else{
            return null;
        }

    }
}
